
package cn.edu.shengda;

class Cone<E> { 
   double height;
   E bottom;           //用泛型类E声明对象bottom
   public Cone (E b) {
      bottom=b;   
   }
   public void setHeight(double h) {
      height=h;
   }
   public double computerVolume() {
      String s=bottom.toString();//泛型变量只能调用从Object类继承的或重写的方法
      double area=Double.parseDouble(s); 
      return 1.0/3.0*area*height; 
   }
}

class Rect {
   double sideA,sideB,area; 
   Rect(double a,double b) {
     sideA=a;
     sideB=b;
   } 
   public String toString() {
       System.out.println("Rect 的toString");
      area=sideA*sideB;
      return ""+area;
   }
}

class Circle {
   double area,radius; 
   Circle(double r) {
      radius=r;
   } 
   public String toString() { //重写Object类的toString()方法
       System.out.println("Cirle 的toString");
      area=radius*radius*Math.PI;
      return ""+area;
   }
}

public class Example15_1 {
   public static void main(String args[]) {
      Circle circle=new Circle(10);
      Cone<Circle> coneOne=new Cone<Circle>(circle);//创建一个（圆）锥对象  
      coneOne.setHeight(16);
      System.out.println(coneOne.computerVolume());
      Rect rect=new Rect(15,23);
      Cone<Rect> coneTwo=new Cone<Rect>(rect);//创建一个（方）锥对象
      coneTwo.setHeight(98); 
      //double s=coneTwo.computerVolume();
      System.out.println(coneTwo.computerVolume());
  }
}
